<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>map_perf.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_labs_structs_map_perf.js.html">map_perf.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2012 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Performance test for goog.structs.Map and
<a name="line17"></a> * goog.labs.structs.Map. To run this test fairly, you would have to
<a name="line18"></a> * compile this via JsCompiler (with --export_test_functions), and
<a name="line19"></a> * pull the compiled JS into an empty HTML file.
<a name="line20"></a> */
<a name="line21"></a>
<a name="line22"></a>goog.provide(&#39;goog.labs.structs.mapPerf&#39;);
<a name="line23"></a>
<a name="line24"></a>goog.require(&#39;goog.dom&#39;);
<a name="line25"></a>goog.require(&#39;goog.labs.structs.Map&#39;);
<a name="line26"></a>goog.require(&#39;goog.structs.Map&#39;);
<a name="line27"></a>goog.require(&#39;goog.testing.PerformanceTable&#39;);
<a name="line28"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line29"></a>
<a name="line30"></a>goog.scope(function() {
<a name="line31"></a>var mapPerf = goog.labs.structs.mapPerf;
<a name="line32"></a>
<a name="line33"></a>
<a name="line34"></a>/**
<a name="line35"></a> * @typedef {goog.labs.structs.Map|goog.structs.Map}
<a name="line36"></a> */
<a name="line37"></a>mapPerf.MapType;
<a name="line38"></a>
<a name="line39"></a>
<a name="line40"></a>/**
<a name="line41"></a> * @type {goog.testing.PerformanceTable}
<a name="line42"></a> */
<a name="line43"></a>mapPerf.perfTable;
<a name="line44"></a>
<a name="line45"></a>
<a name="line46"></a>/**
<a name="line47"></a> * A key list. This maps loop index to key name to be used during
<a name="line48"></a> * benchmark. This ensure that we do not need to pay the cost of
<a name="line49"></a> * string concatenation/GC whenever we derive a key from loop index.
<a name="line50"></a> *
<a name="line51"></a> * This is filled once in setUpPage and then remain unchanged for the
<a name="line52"></a> * rest of the test case.
<a name="line53"></a> *
<a name="line54"></a> * @type {Array}
<a name="line55"></a> */
<a name="line56"></a>mapPerf.keyList = [];
<a name="line57"></a>
<a name="line58"></a>
<a name="line59"></a>/**
<a name="line60"></a> * Maxium number of keys in keyList (and, by extension, the map under
<a name="line61"></a> * test).
<a name="line62"></a> * @type {number}
<a name="line63"></a> */
<a name="line64"></a>mapPerf.MAX_NUM_KEY = 10000;
<a name="line65"></a>
<a name="line66"></a>
<a name="line67"></a>/**
<a name="line68"></a> * Fills the given map with generated key-value pair.
<a name="line69"></a> * @param {mapPerf.MapType} map The map to fill.
<a name="line70"></a> * @param {number} numKeys The number of key-value pair to fill.
<a name="line71"></a> */
<a name="line72"></a>mapPerf.fillMap = function(map, numKeys) {
<a name="line73"></a>  goog.asserts.assert(numKeys &lt;= mapPerf.MAX_NUM_KEY);
<a name="line74"></a>
<a name="line75"></a>  for (var i = 0; i &lt; numKeys; ++i) {
<a name="line76"></a>    map.set(mapPerf.keyList[i], i);
<a name="line77"></a>  }
<a name="line78"></a>};
<a name="line79"></a>
<a name="line80"></a>
<a name="line81"></a>/**
<a name="line82"></a> * Primes the given map with deletion of keys.
<a name="line83"></a> * @param {mapPerf.MapType} map The map to prime.
<a name="line84"></a> * @return {mapPerf.MapType} The primed map (for chaining).
<a name="line85"></a> */
<a name="line86"></a>mapPerf.primeMapWithDeletion = function(map) {
<a name="line87"></a>  for (var i = 0; i &lt; 1000; ++i) {
<a name="line88"></a>    map.set(mapPerf.keyList[i], i);
<a name="line89"></a>  }
<a name="line90"></a>  for (var i = 0; i &lt; 1000; ++i) {
<a name="line91"></a>    map.remove(mapPerf.keyList[i]);
<a name="line92"></a>  }
<a name="line93"></a>  return map;
<a name="line94"></a>};
<a name="line95"></a>
<a name="line96"></a>
<a name="line97"></a>/**
<a name="line98"></a> * Runs performance test for Map#get with the given map.
<a name="line99"></a> * @param {mapPerf.MapType} map The map to stress.
<a name="line100"></a> * @param {string} message Message to be put in performance table.
<a name="line101"></a> */
<a name="line102"></a>mapPerf.runPerformanceTestForMapGet = function(map, message) {
<a name="line103"></a>  mapPerf.fillMap(map, 10000);
<a name="line104"></a>
<a name="line105"></a>  mapPerf.perfTable.run(
<a name="line106"></a>      function() {
<a name="line107"></a>        // Creates local alias for map and keyList.
<a name="line108"></a>        var localMap = map;
<a name="line109"></a>        var localKeyList = mapPerf.keyList;
<a name="line110"></a>
<a name="line111"></a>        for (var i = 0; i &lt; 500; ++i) {
<a name="line112"></a>          var sum = 0;
<a name="line113"></a>          for (var j = 0; j &lt; 10000; ++j) {
<a name="line114"></a>            sum += localMap.get(localKeyList[j]);
<a name="line115"></a>          }
<a name="line116"></a>        }
<a name="line117"></a>      },
<a name="line118"></a>      message);
<a name="line119"></a>};
<a name="line120"></a>
<a name="line121"></a>
<a name="line122"></a>/**
<a name="line123"></a> * Runs performance test for Map#set with the given map.
<a name="line124"></a> * @param {mapPerf.MapType} map The map to stress.
<a name="line125"></a> * @param {string} message Message to be put in performance table.
<a name="line126"></a> */
<a name="line127"></a>mapPerf.runPerformanceTestForMapSet = function(map, message) {
<a name="line128"></a>  mapPerf.perfTable.run(
<a name="line129"></a>      function() {
<a name="line130"></a>        // Creates local alias for map and keyList.
<a name="line131"></a>        var localMap = map;
<a name="line132"></a>        var localKeyList = mapPerf.keyList;
<a name="line133"></a>
<a name="line134"></a>        for (var i = 0; i &lt; 500; ++i) {
<a name="line135"></a>          for (var j = 0; j &lt; 10000; ++j) {
<a name="line136"></a>            localMap.set(localKeyList[i], i);
<a name="line137"></a>          }
<a name="line138"></a>        }
<a name="line139"></a>      },
<a name="line140"></a>      message);
<a name="line141"></a>};
<a name="line142"></a>
<a name="line143"></a>
<a name="line144"></a>goog.global[&#39;setUpPage&#39;] = function() {
<a name="line145"></a>  var content = goog.dom.createDom(&#39;div&#39;);
<a name="line146"></a>  goog.dom.insertChildAt(document.body, content, 0);
<a name="line147"></a>  var ua = navigator.userAgent;
<a name="line148"></a>  content.innerHTML =
<a name="line149"></a>      &#39;&lt;h1&gt;Closure Performance Tests - Map&lt;/h1&gt;&#39; +
<a name="line150"></a>      &#39;&lt;p&gt;&lt;strong&gt;User-agent: &lt;/strong&gt;&lt;span id=&quot;ua&quot;&gt;&#39; + ua + &#39;&lt;/span&gt;&lt;/p&gt;&#39; +
<a name="line151"></a>      &#39;&lt;div id=&quot;perf-table&quot;&gt;&lt;/div&gt;&#39; +
<a name="line152"></a>      &#39;&lt;hr&gt;&#39;;
<a name="line153"></a>
<a name="line154"></a>  mapPerf.perfTable = new goog.testing.PerformanceTable(
<a name="line155"></a>      goog.dom.getElement(&#39;perf-table&#39;));
<a name="line156"></a>
<a name="line157"></a>  // Fills keyList.
<a name="line158"></a>  for (var i = 0; i &lt; mapPerf.MAX_NUM_KEY; ++i) {
<a name="line159"></a>    mapPerf.keyList.push(&#39;k&#39; + i);
<a name="line160"></a>  }
<a name="line161"></a>};
<a name="line162"></a>
<a name="line163"></a>
<a name="line164"></a>goog.global[&#39;testGetFromLabsMap&#39;] = function() {
<a name="line165"></a>  mapPerf.runPerformanceTestForMapGet(
<a name="line166"></a>      new goog.labs.structs.Map(), &#39;#get: no previous deletion (Labs)&#39;);
<a name="line167"></a>};
<a name="line168"></a>
<a name="line169"></a>
<a name="line170"></a>goog.global[&#39;testGetFromOriginalMap&#39;] = function() {
<a name="line171"></a>  mapPerf.runPerformanceTestForMapGet(
<a name="line172"></a>      new goog.structs.Map(), &#39;#get: no previous deletion (Original)&#39;);
<a name="line173"></a>};
<a name="line174"></a>
<a name="line175"></a>
<a name="line176"></a>goog.global[&#39;testGetWithPreviousDeletionFromLabsMap&#39;] = function() {
<a name="line177"></a>  mapPerf.runPerformanceTestForMapGet(
<a name="line178"></a>      mapPerf.primeMapWithDeletion(new goog.labs.structs.Map()),
<a name="line179"></a>      &#39;#get: with previous deletion (Labs)&#39;);
<a name="line180"></a>};
<a name="line181"></a>
<a name="line182"></a>
<a name="line183"></a>goog.global[&#39;testGetWithPreviousDeletionFromOriginalMap&#39;] = function() {
<a name="line184"></a>  mapPerf.runPerformanceTestForMapGet(
<a name="line185"></a>      mapPerf.primeMapWithDeletion(new goog.structs.Map()),
<a name="line186"></a>      &#39;#get: with previous deletion (Original)&#39;);
<a name="line187"></a>};
<a name="line188"></a>
<a name="line189"></a>
<a name="line190"></a>goog.global[&#39;testSetFromLabsMap&#39;] = function() {
<a name="line191"></a>  mapPerf.runPerformanceTestForMapSet(
<a name="line192"></a>      new goog.labs.structs.Map(), &#39;#set: no previous deletion (Labs)&#39;);
<a name="line193"></a>};
<a name="line194"></a>
<a name="line195"></a>
<a name="line196"></a>goog.global[&#39;testSetFromOriginalMap&#39;] = function() {
<a name="line197"></a>  mapPerf.runPerformanceTestForMapSet(
<a name="line198"></a>      new goog.structs.Map(), &#39;#set: no previous deletion (Original)&#39;);
<a name="line199"></a>};
<a name="line200"></a>
<a name="line201"></a>});  // goog.scope
</pre>


</body>
</html>
